﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.WorryFreeMoveSuiteConfigTemplate;
using BCDto.TMS.WorryFreeMoveSuiteConfigTemplate;
using BCEntity.TMS.WorryFreeMoveSuiteConfigTemplate;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCDto.Sim.Admin.Admin;
using BCData.TMS.VehicleBusinessMapping;

namespace BCService.TMS.WorryFreeMoveSuiteConfigTemplate
{
	public class WorryFreeMoveSuiteConfigTemplateService : IWorryFreeMoveSuiteConfigTemplateService
	{
		private readonly IRedisService redisService;
		private readonly IWorryFreeMoveSuiteConfigTemplateData worryFreeMoveSuiteConfigTemplateData;
		private readonly IVehicleBusinessMappingData vehicleBusinessMappingData;
		private readonly string key = string.Format("{0}", EntityNameConst.WorryFreeMoveSuiteConfigTemplateEntity);

		public WorryFreeMoveSuiteConfigTemplateService(
			IRedisService redisService,
			IWorryFreeMoveSuiteConfigTemplateData worryFreeMoveSuiteConfigTemplateData,
			IVehicleBusinessMappingData vehicleBusinessMappingData)
		{
			this.redisService = redisService;
			this.worryFreeMoveSuiteConfigTemplateData = worryFreeMoveSuiteConfigTemplateData;
			this.vehicleBusinessMappingData = vehicleBusinessMappingData;
		}

		#region 检查无忧搬家套餐配置模板是否存在
		public bool Exists(int worryFreeMoveSuiteConfigTemplateId)
		{
			return this.worryFreeMoveSuiteConfigTemplateData.Exists(worryFreeMoveSuiteConfigTemplateId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加无忧搬家套餐配置模板
		public WorryFreeMoveSuiteConfigTemplateDto Add(AdminDto adminDto,WorryFreeMoveSuiteConfigTemplateRequestDto requestDto)
		{
			if (adminDto.AgentRecord != null)
			{
				throw new ArgumentException("代理商无权配置此模式模板");
			}
			if (worryFreeMoveSuiteConfigTemplateData.Exists(requestDto.VehicleBusinessMappingId, requestDto.SuiteName).GetAwaiter().GetResult())
			{
				throw new ArgumentException("此套餐配置已存在!");
			}
			var vehicleTransportBusinessConfigEntity = this.vehicleBusinessMappingData.GetEntity(requestDto.VehicleBusinessMappingId).Result;
			if (vehicleTransportBusinessConfigEntity == null)
			{
				throw new ArgumentException("车辆与业务配置信息不存在!");
			}
			
			var entity = requestDto.As<WorryFreeMoveSuiteConfigTemplateEntity>();
			entity.CreateAdminName = adminDto.FullName;
			entity.CreateAdminId = adminDto.AdminId;
			entity.VehicleTypeId = vehicleTransportBusinessConfigEntity.VehicleTypeId;

			return this.worryFreeMoveSuiteConfigTemplateData.Add(entity).GetAwaiter().GetResult().As<WorryFreeMoveSuiteConfigTemplateDto>();
		}
		#endregion

		#region 修改无忧搬家套餐配置模板
		public WorryFreeMoveSuiteConfigTemplateDto Update(AdminDto adminDto, WorryFreeMoveSuiteConfigTemplatePutDto putDto)
		{
			if (!this.Exists(putDto.WorryFreeMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			if (adminDto.AgentRecord != null)
			{
				throw new ArgumentException("代理商无权配置此模式模板");
			}
			if (worryFreeMoveSuiteConfigTemplateData.Exists(putDto.VehicleBusinessMappingId, putDto.SuiteName).GetAwaiter().GetResult())
			{
				var oldEntity = worryFreeMoveSuiteConfigTemplateData.GetEntity(putDto.VehicleBusinessMappingId, putDto.SuiteName).GetAwaiter().GetResult();
                if (oldEntity.WorryFreeMoveSuiteConfigTemplateId != putDto.WorryFreeMoveSuiteConfigTemplateId)
                {
					throw new ArgumentException("此套餐配置已存在!");
				}
			}
			var vehicleTransportBusinessConfigEntity = this.vehicleBusinessMappingData.GetEntity(putDto.VehicleBusinessMappingId).Result;
			if (vehicleTransportBusinessConfigEntity == null)
			{
				throw new ArgumentException("车辆与业务配置信息不存在!");
			}

			var entity = putDto.As<WorryFreeMoveSuiteConfigTemplateEntity>();
			entity.EditAdminName = adminDto.FullName;
			entity.EditAdminId = adminDto.AdminId;
			entity.VehicleTypeId = vehicleTransportBusinessConfigEntity.VehicleTypeId;

			var result = this.worryFreeMoveSuiteConfigTemplateData.Update(entity).GetAwaiter().GetResult().As<WorryFreeMoveSuiteConfigTemplateDto>();
			string cacheKey = this.redisService.GetKey(key, entity.WorryFreeMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除无忧搬家套餐配置模板
		public bool Delete(int worryFreeMoveSuiteConfigTemplateId)
		{
			if (!this.Exists(worryFreeMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.worryFreeMoveSuiteConfigTemplateData.Delete(worryFreeMoveSuiteConfigTemplateId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, worryFreeMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取无忧搬家套餐配置模板
		public WorryFreeMoveSuiteConfigTemplateDto Get(int worryFreeMoveSuiteConfigTemplateId)
		{
			string cacheKey = this.redisService.GetKey(key, worryFreeMoveSuiteConfigTemplateId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.worryFreeMoveSuiteConfigTemplateData.GetEntity(worryFreeMoveSuiteConfigTemplateId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<WorryFreeMoveSuiteConfigTemplateDto>();
		}
		#endregion

		#region 获取无忧搬家套餐配置模板列表
		public IEnumerable<WorryFreeMoveSuiteConfigTemplateDto> GetList()
		{
			var result = this.redisService.TryGet(key, () =>
			{
				return this.worryFreeMoveSuiteConfigTemplateData.GetList().GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<WorryFreeMoveSuiteConfigTemplateDto>>();
		}
		#endregion

		#region 获取无忧搬家套餐配置模板分页
		public PageableList<WorryFreeMoveSuiteConfigTemplateDto> Query(string suiteName, bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, suiteName, enabled, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.worryFreeMoveSuiteConfigTemplateData.Query(suiteName, enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<WorryFreeMoveSuiteConfigTemplateDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<WorryFreeMoveSuiteConfigTemplateDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion

		#region 启用无忧搬家套餐配置模板
		public bool Enabled(AdminDto adminDto, int worryFreeMoveSuiteConfigTemplateId)
		{
			if (!this.Exists(worryFreeMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.worryFreeMoveSuiteConfigTemplateData.Enabled(worryFreeMoveSuiteConfigTemplateId, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, worryFreeMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 禁用无忧搬家套餐配置模板
		public bool Disabled(AdminDto adminDto, int worryFreeMoveSuiteConfigTemplateId)
		{
			if (!this.Exists(worryFreeMoveSuiteConfigTemplateId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var result = this.worryFreeMoveSuiteConfigTemplateData.Disabled(worryFreeMoveSuiteConfigTemplateId, adminDto.AdminId, adminDto.FullName).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, worryFreeMoveSuiteConfigTemplateId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion
	}
}